<template>
  <div class="bg">
    <el-card class="box-card">
      <el-row type="flex" class="row-bg" justify="end">
        <el-col :span="6" :offset="24">
          <el-input style="width:300px" v-model="selectCode" placeholder="请输入商家名称/联系人/手机号码查询"></el-input>
        </el-col>
        <el-col :span="6" :offset="1">
          <el-date-picker
            v-model="firstdata"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择开始日期"
          ></el-date-picker>
        </el-col>
        <el-col :span="6" :offset="1">
          <el-date-picker
            v-model="endtime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择开始日期"
          ></el-date-picker>
        </el-col>
        <el-col :span="6" :offset="1">
          <div style="display:flex">
            <el-button type="primary" @click="searchDate">查询</el-button>
            <el-button type="success" @click="reset">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <!-- 表格展示 -->
    <el-card class="card">
      <div slot="header">
        <el-row type="flex" justify="space-between">
          <el-col :span="22">
            <div class="merchantData">
              <div style="margin-right:20px">
                <span class="addmerchant">昨日新增商家</span>
                <span class="merchantNum">{{merchantTotal.yesterDayNumber}}</span>
                <span class="addmerchant">家</span>
              </div>
              <div style="margin-right:20px">
                <span class="addmerchant">近三天新增</span>
                <span class="merchantNum">{{merchantTotal.threeDayNumber}}</span>
                <span class="addmerchant">家</span>
              </div>
              <div style="margin-right:20px">
                <span class="addmerchant">累计入驻商家</span>
                <span class="merchantNum">{{merchantTotal.totalDayNumber}}</span>
                <span class="addmerchant">家</span>
              </div>
            </div>
          </el-col>
          <el-col :span="2">
            <el-button type="success" @click="exports">导出数据</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格展示 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="index" width="100" label="序号" align="center"></el-table-column>
        <el-table-column prop="businessContact" label="联系人" align="center"></el-table-column>
        <el-table-column prop="businessContactPhone" label="联系电话" align="center"></el-table-column>
        <el-table-column prop="businessName" label="商家名称" align="center"></el-table-column>
        <el-table-column prop="businessAddress" label="详细地址" align="center"></el-table-column>
        <el-table-column prop="businessHours" label="营业时间" align="center"></el-table-column>
        <el-table-column prop="businessIntroduced" label="服务介绍" align="center"></el-table-column>
        <el-table-column label="店铺照" align="center">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 80px" :src="scope.row.businessBanner[0]"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="店铺封面" align="center">
          <template slot-scope="scope">
            <el-popover placement="top-start" width="200" trigger="hover">
              <el-image
                slot="reference"
                :src="scope.row.businessCoverImg"
                :alt="scope.row.businessCoverImg"
                style="width: 100px; height: 80px"
              ></el-image>
              <el-image style="width: 100%" :src="scope.row.businessCoverImg"></el-image>
            </el-popover>
            <!-- <el-image style="width: 100px; height: 80px" :src="scope.row.businessCoverImg"></el-image> -->
          </template>
        </el-table-column>
        <el-table-column label="营业执照" align="center">
          <template slot-scope="scope">
            <el-popover placement="top-start" width="200" trigger="hover">
              <el-image
                slot="reference"
                :src="scope.row.businessLicense"
                :alt="scope.row.businessLicense"
                style="width: 100px; height: 80px"
              ></el-image>
              <el-image style="width: 100%" :src="scope.row.businessLicense"></el-image>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="businessStatus" label="状态" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.businessStatus==1?'正常':scope.row.businessStatus==2?'冻结':'待审核'}}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="primary" size="small">查看/编辑</el-button>
            <el-button @click="deletlist(scope.row)" type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="total,  prev, pager, next, jumper"
        :total="total"
        :current-page="current"
        @current-change="handleCurrentChange"
        style="margin-top:24px"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { export2Excel } from "@/components/commom.js";
export default {
  data() {
    return {
      value1: "",
      downloadLoading: false,
      tableData: [],
      merchantTotal: {}, //商家统计信息
      total: 0, //总条数
      current: 1, //第一页的数据加载
      selectCode: "", //查询条件
      firstdata: "", //开始时间
      endtime: "" //结束时间
    };
  },
  mounted() {
    this.merchantList();
    this.merchantMsgtotal();
  },
  methods: {
    // 获取商户列表
    merchantList() {
      let param = {
        size: 10,
        current: this.current,
        selectCode: this.selectCode,
        startDate: this.firstdata,
        endDate: this.endtime,
        status: 1
      };
      this.$http.article.merchant(param).then(res => {
        this.tableData = res.result.records; //加载数据
        this.total = res.result.total; //总条数
        console.log("获取商户列表", res);
        //显示每页展示的数量
        res.result.records.forEach((item, index) => {
          //res.data.data.list 表格数据     forEach为每一项添加数据
          item.index = index + 1 + (this.current - 1) * 10;
          // this.currentPage当前页  this.pageParams.size 每页显示多少    :page-sizes="[10,20,30,50]" 分页大小

          return item;
        });
      });
    },
    // 获取商家统计信息
    merchantMsgtotal() {
      this.$http.article.getmerchantMsg().then(res => {
        this.merchantTotal = res.result;
        console.log("获取商家统计信息", res);
      });
    },
    // 查询功能
    searchDate() {
      this.current = 1; //模糊查询
      this.merchantList();
    },
    // 分页功能
    handleCurrentChange(val) {
      this.current = val;
      this.merchantList();
    },
    // 重置功能
    reset() {
      this.selectCode = ""; //查询条件
      this.firstdata = ""; //开始时间
      this.endtime = ""; //结束时间
      // 重新加载数据
      this.merchantList();
    },
    // 删除功能
    deletlist(val) {
      console.log("删除", val.businessId);
      this.$confirm("是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.article
            .deletmerchant({ petBusinessId: val.businessId })
            .then(res => {
              console.log("删除功能", res);
              if (res.code == 200) {
                /*--------删除成功之后获取上一页的数据,避免是否为空--------*/

                //此时已经实现删除操作，所以total的值需要减size，Math.ceil是向上取整，保证始终大于等于0
                const totalPage = Math.ceil((this.total - 1) / 10);

                console.log("totalPage", totalPage);

                //将当前页码与删除数据之后的总页数进行比较，避免当前页码不存在
                const pagenum =
                  this.current > totalPage ? totalPage : this.current;

                console.log("获取总条数", pagenum);

                //避免pagenum变为0
                this.current = pagenum < 1 ? 1 : pagenum;
                // 提示弹窗
                this.$message.success("删除成功!");
                this.merchantList();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //导出功能
    exports() {
      console.log("导出");

      const hader = [
        {
          title: "联系人",
          date: "businessContact"
        },
        {
          title: "联系电话",
          date: "businessContactPhone"
        },
        {
          title: "商家名称",
          date: "businessName"
        },
        {
          title: "详细地址",
          date: "businessAddress"
        },
        {
          title: "营业时间",
          date: "businessHours"
        },
        {
          title: "服务介绍",
          date: "businessIntroduced"
        },
        {
          title: "店铺照",
          date: "businessBanner"
        },
        {
          title: "店铺封面",
          date: "businessCoverImg"
        },
        {
          title: "营业执照",
          date: "businessLicense"
        },
        {
          title: "状态",
          date: "businessStatus"
        }
      ];

      const excelName = "商家列表";
      let param = {
        size: this.total,
        status: 1,
        current: 1
      };
      this.$http.article.merchant(param).then(res => {
        const list = res.result.records.map(item => {
          console.log("导出数据", item);
          //因为有枚举类型，所以这里对数据进行处理
          if (item.businessStatus) {
            item.businessStatus =
              item.businessStatus == 1
                ? "正常"
                : item.businessStatus == 2
                ? "无效"
                : "待审核";
          }
          return item;
        });
        export2Excel(hader, list, excelName);
      });
    },

    // 查看商户列表
    handleClick(val) {
      console.log("查看商户列表", val);
      this.$router.push({
        path: "/home/lookmerchant",
        query: {
          businessId: val.businessId
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.merchantData {
  display: flex;
  align-items: center;
  padding-top: 14px;
  .addmerchant {
    color: #409eff;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 400;
  }
  .merchantNum {
    color: #f23a3a;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 800;
  }
}

.clearfix {
  padding-bottom: 20px;
}
.bg {
  width: 100%;
  height: 100%;
  background-color: #f4f7fd;
  padding-top: 10px;
}
.box-card {
  width: 98%;
  height: 80px;
  margin: 0 auto;
}
.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;
}
</style>